package com.cqc.smssdkdemo02;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.util.HashMap;

import cn.smssdk.EventHandler;
import cn.smssdk.SMSSDK;
import cn.smssdk.gui.ContactsPage;
import cn.smssdk.gui.RegisterPage;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private static final String TAG = "MainActivity";
    private Context context;
    private Button btn1;
    private Button btn2;
    private Button btn3;
    private Button btn4;
    private EditText et1;
    private EditText et2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        context = this;

        btn1 = (Button) findViewById(R.id.btn1);
        btn2 = (Button) findViewById(R.id.btn2);
        btn3 = (Button) findViewById(R.id.btn3);
        btn4 = (Button) findViewById(R.id.btn4);

        et1 = (EditText) findViewById(R.id.et1);
        et2 = (EditText) findViewById(R.id.et2);

        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
        btn3.setOnClickListener(this);
        btn4.setOnClickListener(this);

        //注册回调
        SMSSDK.registerEventHandler(eventHandler);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn1://验证码
                opeRegisterPager();
                break;
            case R.id.btn2:
                openContractsPager();
                break;
            case R.id.btn3://获取验证码
                String phone = et1.getText().toString().trim();
                if (TextUtils.isEmpty(phone)) {
                    Toast.makeText(context, "手机号码不能为空", Toast.LENGTH_SHORT).show();
                } else {
                    SMSSDK.getVerificationCode("86", phone);
                    Toast.makeText(context, "手机号是：" + phone, Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.btn4://发送验证码
                String phone2 = et1.getText().toString().trim();
                String code = et2.getText().toString().trim();
                if (TextUtils.isEmpty(phone2) || TextUtils.isEmpty(code)) {
                    Toast.makeText(context, "手机号和验证码均不能为空", Toast.LENGTH_SHORT).show();
                    SMSSDK.submitVerificationCode("+86", phone2, code);
                } else {
                    Toast.makeText(context, "手机号是：" + phone2 + "验证码是：" + code, Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }


    //防止内存泄漏
    @Override
    protected void onDestroy() {
        super.onDestroy();
        SMSSDK.unregisterEventHandler(eventHandler);
    }

    private EventHandler eventHandler = new EventHandler() {
        @Override
        public void afterEvent(int event, int result, Object data) {
            super.afterEvent(event, result, data);
            if (result == SMSSDK.RESULT_COMPLETE) {
                //回调完成
                if (event == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE) {
                    //提交验证码成功
                    @SuppressWarnings("unchecked") HashMap<String, Object> phoneMap = (HashMap<String, Object>) data;
                    String country = (String) phoneMap.get("country");
                    String phone = (String) phoneMap.get("phone");
                    Log.d(TAG, "提交验证码成功--country=" + country + "--phone" + phone);
                } else if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE) {
                    //获取验证码成功
                    Log.d(TAG, "获取验证码成功");
                } else if (event == SMSSDK.EVENT_GET_SUPPORTED_COUNTRIES) {
                    //返回支持发送验证码的国家列表
                }
            } else {
                ((Throwable) data).printStackTrace();
            }
        }
    };

    private void openContractsPager() {
        //打开通信录好友列表页面
        ContactsPage contactsPage = new ContactsPage();
        contactsPage.show(context);
    }

    private void opeRegisterPager() {
        //打开注册页面,这个页面就是我们在AndroidManifest.xml中添加的节点com.mob.tools.MobUIShell
        RegisterPage registerPage = new RegisterPage();
        registerPage.setRegisterCallback(new EventHandler() {
            public void afterEvent(int event, int result, Object data) {
                // 解析注册结果
                if (result == SMSSDK.RESULT_COMPLETE) {
                    @SuppressWarnings("unchecked") HashMap<String, Object> phoneMap = (HashMap<String, Object>) data;
                    String country = (String) phoneMap.get("country");
                    String phone = (String) phoneMap.get("phone");
                    Log.d(TAG, "opeRegisterPager()--country=" + country + "--phone" + phone);
                }
            }
        });
        registerPage.show(context);
    }
}
